package gl.android.biyiqing.fragment;

import gl.adapter.SimpleAdapter;
import gl.android.biyiqing.R;
import gl.android.biyiqing.SearchActivity;
import gl.android.biyiqing.SearchOptionActivity;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.uitls.AndroidUtil;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.edmodo.rangebar.RangeBar;

public class SearchFragment extends BaseFragment {

	private static final String TYPE_HOME = "1";
	private static final String SEX = "2";
	private static final String SEX_RECOMMND = "1";

	// Content View Elements

	private EditText et_search;
	private View iv_search;
	private GridView gv_search;
	protected AlertDialog dialog;

	// End Of Content View Elements

	private void bindViews() {

		et_search = (EditText) findViewById(R.id.et_search);
		iv_search = findViewById(R.id.iv_search);
		gv_search = (GridView) findViewById(R.id.gv_search);
		iv_search.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (et_search.getText() != null) {
					goSearchActivity("10", et_search.getText().toString());
				}

			}
		});
		et_search.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView textView, int id,
					KeyEvent keyEvent) {
				if (id == R.id.search || id == EditorInfo.IME_ACTION_SEARCH) {
					iv_search.performClick();
					return true;
				}
				return false;
			}
		});

	}

	public static SearchFragment newInstance() {
		SearchFragment fragment = new SearchFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public SearchFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_search, container, false);
		bindViews();
		initData();
		return rootView;
	}

	private void initData() {
		final ArrayList<HashMap<String, String>> map = new ArrayList<HashMap<String, String>>();
		addSearchItem(map, R.drawable.ic_search_btn_condition_n, "条件搜索", "1",
				0xfff0dc01);
		addSearchItem(map, R.drawable.ic_search_btn_interest_n, "兴趣分类", "2",
				0xfffc5253);
		addSearchItem(map, R.drawable.ic_search_btn_new_n, "最新会员", "3",
				0xffce8457);
		addSearchItem(map, R.drawable.ic_search_btn_online_n, "在线会员", "4",
				0xff79c950);
		addSearchItem(map, R.drawable.ic_search_btn_fellow_n, "老乡会员", "5",
				0xffb366f2);
		addSearchItem(map, R.drawable.ic_search_btn_unit_n, "单位会员", "6",
				0xff01b5d5);
		addSearchItem(map, R.drawable.ic_search_btn_car_n, "有房有车", "7",
				0xff3eecb9);
		addSearchItem(map, R.drawable.ic_search_btn_photo_n, "有照片", "8",
				0xff999999);
		addSearchItem(map, R.drawable.ic_search_btn_civil_n, "距离最近", "9",
				0xffff8e35);
		initGridView(gv_search, map, R.layout.item_grid_menu, new String[] {
				"username", "avatar", SimpleAdapter.BGCOLOR }, new int[] {
				R.id.tv_nick, R.id.iv_avator, R.id.rl_iv });
		gv_search.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				final String type = (String) map.get(arg2).get("type");
				final String data = (String) map.get(arg2).get("data");
				if (arg2 == 0) {
					goSearchOptinActivity(type, data);
				} else if (arg2 == 1) {
					if (dialog == null) {
						final String[] ss = BYQUserInfo
								.getReadableArray("兴趣爱好");
						final ListView listview = new ListView(getActivity());
						ArrayAdapter<String> adapter = new ArrayAdapter<String>(
								getActivity(), R.layout.item_list_xingqu,
								android.R.id.text1);
						int i = 0;
						for (String s : ss) {
							if (i == 0) {
								i++;
								continue;
							}
							adapter.add(s);
							i++;
						}
						listview.setAdapter(adapter);
						listview.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {
								dialog.dismiss();
								goSearchActivity(type, "" + position);
							}
						});
						dialog = AndroidUtil.genCustomViewDialog(getActivity(),
								listview).show();
					} else {
						dialog.show();
					}

				} else {
					goSearchActivity(type, data);
				}
			}

		});

	}

	int REQUEST_CODE = 999;

	private void goSearchOptinActivity(String type, String data) {
		Intent intent = new Intent(getActivity(), SearchOptionActivity.class);
		Bundle extras = new Bundle();
		intent.putExtras(extras);
		startActivityForResult(intent, REQUEST_CODE);
	}

//	@Override
//	public void onActivityResult(int requestCode, int resultCode, Intent data) {
//		if (REQUEST_CODE == requestCode && resultCode == Activity.RESULT_OK) {
//			goSearchActivity("1", data.getExtras().getString("data"));
//		}
//	}

	private void goSearchActivity(String type, String data) {
		Intent intent = new Intent(getActivity(), SearchActivity.class);
		Bundle extras = new Bundle();
		extras.putString("type", type);
		extras.putString("data", data);
		intent.putExtras(extras);
		startActivity(intent);
	}

	private void addSearchItem(ArrayList<HashMap<String, String>> map, int avatar,
			String title, String type, int bgcolor) {
		HashMap<String, String> mHashMap = new HashMap<String, String>();
		mHashMap.put("avatar", String.valueOf(avatar));
		mHashMap.put("username", title);
		mHashMap.put("type", type);
		mHashMap.put(SimpleAdapter.BGCOLOR, String.valueOf(bgcolor));
		map.add(mHashMap);
	}

	private void initGridView(final GridView adpateview,
			ArrayList<HashMap<String, String>> pars, final int itemLayout,
			final String[] from, final int[] to) {
		adpateview.setAdapter(new SimpleAdapter(getActivity(), pars,
				itemLayout, from, to));

	}
}
